package com.smssalama.storage.sms;

import javax.microedition.rms.RecordFilter;

import com.smssalama.storage.framework.ObjectStorageException;
import com.smssalama.storage.framework.Storable;
import com.smssalama.storage.framework.StorableFilter;

public class SMSMessageFilter implements RecordFilter, StorableFilter {
	public static final byte FF_OUT = 1;
	public static final byte FF_OUT_PENDING = 2;
	public static final byte FF_OUT_SENT = 4;
	public static final byte FF_OUT_FAILED = 8;
	public static final byte FF_IN = 16;
	public static final byte FF_IN_ENCRYPTED = 32 | SMSMessageFilter.FF_IN;
	public static final byte FF_IN_DECRYPTED = 64 | SMSMessageFilter.FF_IN;

	private String phoneNumber;
	private byte[] flags;

	public static final SMSMessageFilter PASSTHROUGH = new SMSMessageFilter();

	protected SMSMessageFilter() {
	}

	public SMSMessageFilter(String phoneNumber) {
		this.phoneNumber = phoneNumber;
		this.flags = null;
	}

	public SMSMessageFilter(String phoneNumber, byte[] flags) {
		this.phoneNumber = phoneNumber;
		this.flags = flags;
	}

	public SMSMessageFilter(byte[] flags) {
		this.phoneNumber = null;
		this.flags = flags;
	}

	public boolean matches(byte[] arg0) {
		SMSMessage msg;
		try {
			msg = (SMSMessage)Storable.init(arg0);
			return matches(msg);
		} catch (ObjectStorageException e) {
			e.printStackTrace();
			return false;
		}		
	}
	public boolean matches(Storable storable) {
		if(!(storable instanceof SMSMessage)){
			return false;
		}
		SMSMessage msg = (SMSMessage)storable;
		if (this.phoneNumber != null) {
			if (!this.phoneNumber.equals(msg.getPhoneNumber())) {
				return false;
			}
		}
		if (this.flags != null) {
			for (int i = 0; i < this.flags.length; i++) {
				switch (this.flags[i]) {
				case FF_OUT:
					if (!(msg instanceof SMSMessageOut)) {
						return false;
					}
					break;
				case FF_OUT_PENDING:
					if (!(msg instanceof SMSMessageOut)) {
						return false;
					}
					if (((SMSMessageOut) msg).getStatus() != SMSMessageOut.PENDING) {
						return false;
					}
					break;
				case FF_OUT_SENT:
					if (!(msg instanceof SMSMessageOut)) {
						return false;
					}
					if (((SMSMessageOut) msg).getStatus() != SMSMessageOut.SENT) {
						return false;
					}
				case FF_OUT_FAILED:
					if (!(msg instanceof SMSMessageOut)) {
						return false;
					}
					if (((SMSMessageOut) msg).getStatus() != SMSMessageOut.FAILED) {
						return false;
					}
				case FF_IN:
					if (!(msg instanceof SMSMessageIn)) {
						return false;
					}
					break;
				case FF_IN_ENCRYPTED:
					if (!(msg instanceof SMSMessageIn)) {
						return false;
					}
					if (!((SMSMessageIn) msg).isEncrypted()) {
						return false;
					}
					break;
				case FF_IN_DECRYPTED:
					if (!(msg instanceof SMSMessageIn)) {
						return false;
					}
					if (((SMSMessageIn) msg).isEncrypted()) {
						return false;
					}
					break;
				}
			}
		}
		return true;
	}

	public static RecordFilter getPassFilter() {
		return new RecordFilter() {
			public boolean matches(byte[] arg0) {
				return true;
			}
		};
	}
}
